<?php

namespace Apps\AppApi\Method\Question;
use Apps\AppApi\Method\BaseMethod;
use Apps\Common\Libs\PicUrl;
use Apps\Common\Models\Question;
use Apps\Common\Models\QuestionFollow;
use PhalconPlus\Api\Common\ResultStatus;


/**
 * 问题列表，可通过分类筛选
 * @package Apps\AppApi\Method\GetList
 */
class GetList extends BaseMethod
{
    public function run()
    {
        $orders = $this->getDataItem('orders', 1); // 排序方式（1=》最新提问，2=》最新回复，3=》人气问答）
        $page = intval($this->getDataItem('page',1));
        $is_expert_reply = $this->getDataItem('is_expert_reply', 0); // 专家是否回复
        $city_code = (string)$this->getDataItem('city_code', '0'); // 原型去掉了
        $user_id = intval($this->getDataItem('user_id', 0));
        $keyword = $this->getDataItem('keyword', '');

        $limit = $this->getConfig()->limit;
        $offset = ($page-1) * $limit;

        $columns = [
            'question_id',
            'question_content',
            'question_pics',
            'question_tag',
            'answer_num',
            'question_browsers',
            'question_addtime',
            'is_expert_reply',
            'u.user_id',
            'u.user_nickname',
            'u.user_expert',
            'u.user_cover',
            'u.user_gender',
        ];

        $where = 'question_state = 1';
        switch ($orders) {
            case 2:
                $where .= " and answer_num > 0";
                $orders = 'last_reply_time DESC';
                break;
            case 3:
                $orders = 'question_browsers DESC';
                break;
            default:
                $orders = 'question_addtime DESC';  
        }

        if ($is_expert_reply == 1) {
            $where .= ' and is_expert_reply = 1';
        }
        if ($keyword) {
            $where .= " and question_content like '%{$keyword}%'";
        }
        /*if ($city_code) {
            $where .= " and city_code in('{$city_code}', 0, '')";
        }*/
        $where .= " and city_code in('{$city_code}', 0, '')";

        $list = Question::query()->columns($columns)->leftJoin('\Apps\Common\Models\UserBase', 'u.user_id = \Apps\Common\Models\Question.user_id', 'u')->where($where)->orderBy($orders)->limit($limit, $offset)->execute()->toArray();

        $QuestionFollow = new QuestionFollow();
        foreach ($list as $k => $val) {
            // 是否收藏过
            if ($user_id) {
                $list[$k]['is_collection'] = $QuestionFollow->is_follow($val['question_id'], $user_id);
            } else {
                $list[$k]['is_collection'] = '0';
            }

            $list[$k]['question_tag'] = $val['question_tag'] ? json_decode($val['question_tag'], true) : [];
            $list[$k]['user_cover'] = PicUrl::UserCover($val['user_cover'], $this->getDi());
            $list[$k]['question_content'] = unserialize(base64_decode($val['question_content'])) ?: $val['question_content'];

            if ($val['question_pics']) {
                $question_pics = [];
                foreach (json_decode($val['question_pics'], true) as $key => $value) {
                    $question_pics[] = PicUrl::Question($value, $this->getDi());
                }
                $list[$k]['question_pics'] = $question_pics;
            }
        }

        $this->setResult($list);
    }

}